package com.baor.service.impl;

import com.baor.dao.ClazzDao;
import com.baor.dao.TeacherDao;
import com.baor.domain.TeacherVo;
import com.baor.service.TeacherService;
import com.baor.service.UserService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private UserService userService;


    @Override
    @Transactional
    public AjaxResult<?> insertTeacher(TeacherVo teacherVo) {
        Map<String,Object> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        list.add(Integer.valueOf(teacherVo.getRoleId()));
        map.put("userName",teacherVo.getTeacherName());
        map.put("userPassword",teacherVo.getUserAccount());
        map.put("userAccount",teacherVo.getUserAccount());
        map.put("userRole",list);
        int code = userService.insertUser(map).getCode();
        if(code == 200){
            teacherDao.insert(teacherVo);
            for(int i=0;i<teacherVo.getClassId().size();i++){
                Map<String,Object> map1 = new HashMap<>();
                map1.put("teacherId",teacherVo.getTeacherId());
                map1.put("subjectId",teacherVo.getSubjectId());
                map1.put("classId",teacherVo.getClassId().get(i));
                teacherDao.addClass(map1);
            }
            return AjaxResult.success();
        }else{
            return AjaxResult.error("账号重复");
        }
    }

    @Override
    public AjaxResult<?> selectTeacher(Map<String,Object> map) {
        map.put("pageIndex", (int)map.get("pageSize") * ((int)(map.get("pageIndex")) - 1));
        map.put("pageSize", map.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("teacherList",this.teacherDao.teacherList(map));
        res.put("pageCount",this.teacherDao.pageCount(map));
        return AjaxResult.success(res);
    }

    @Override
    @Transactional
    public AjaxResult<?> updateTeacher(TeacherVo teacherVo) {
        if(teacherVo.getClassId()!= null) {
            Integer subjectId = teacherDao.subject(teacherVo.getTeacherId());
            teacherDao.deleteClass(teacherVo.getTeacherId());
            for (int i = 0; i < teacherVo.getClassId().size(); i++) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("teacherId", teacherVo.getTeacherId());
                map1.put("subjectId", subjectId);
                map1.put("classId", teacherVo.getClassId().get(i));
                teacherDao.addClass(map1);
            }
        }
        if(teacherVo.getSubjectId() != null){
            teacherDao.updateSubject(teacherVo.getTeacherId(), Integer.parseInt(teacherVo.getSubjectId()));
        }
        return AjaxResult.success(this.teacherDao.updateById(teacherVo));
    }

    @Override
    @Transactional
    public AjaxResult<?> deleteTeacher(TeacherVo teacherVo) {
        teacherDao.deleteClass(teacherVo.getTeacherId());
        teacherVo.setDelflag(1);
        return AjaxResult.success(this.teacherDao.updateById(teacherVo));
    }

    @Override
    public AjaxResult<?> teacherInfo(Map<String, Object> teacherId) {
        return AjaxResult.success(this.teacherDao.teacherInfo(teacherId));
    }

    @Override
    public AjaxResult<?> clazzSelect() {
        return AjaxResult.success(this.teacherDao.classSelect());
    }

    @Override
    public AjaxResult<?> subjectSelect() {
        return AjaxResult.success(this.teacherDao.subjectSelect());
    }

    @Override
    public AjaxResult<?> roleSelect() {
        return AjaxResult.success(this.teacherDao.roleSelect());
    }

}


